/* Copyright 2009 Sony Mathew, Minneapolis MN. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at  http://www.apache.org/licenses/LICENSE-2.0  Unless required by applicable law or agreed to in writing, software  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */
package com.xsm.gwt.widgets.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.HTML;

/**
 * The HTMLInclude widget allows you to specify a {@link Source} for HTML from
 *  where HTML content is retreived and embedded as is.
 *  
 * @author Sony Mathew
 */
public class HTMLInclude extends HTML implements Source.Consumer {
    
    private final Source htmlSrc;
    private final String defaultHTML;
    
    /**
     * Convenience method for {@link #HTMLInclude(String, String)}
     *  Uses "Not-Available" as the default HTML.
     *  
     * author Sony Mathew
     */
    public HTMLInclude(String url) {
        this(url, "<b>Not-Available</b>");
    }
    
    /**
     * Convenience method to obtain HTML from an {@link HttpSource}
     * 
     * @see #HTMLInclude(Source, String)
     * 
     * @param url: provide the url whose content is HTML from which this 
     *  component will be built - and {@link HttpSource} will be created from it.  
     *      
     * author Sony Mathew
     */
    public HTMLInclude(String url, String defaultHTML) {
        this(new HttpSource(url), defaultHTML);
    }

    /**
     * The HTML content from the Source is read immediately.
     *  Invoke refresh() to re-load or setHTML(html) explictly if you want to 
     *      override HTML content.
     *  
     * @param htmlSrc: Provide a {@link Source} for the HTML content. 
     *      
     * @param defaultHTML: The default HTML to display if the URL content
     *  could not be read.
     *
     * author Sony Mathew
     */    
    public HTMLInclude(Source htmlSrc, String defaultHTML) {
        this.htmlSrc = htmlSrc;
        this.htmlSrc.addConsumer(this);
        this.defaultHTML = defaultHTML;     
        refresh();
    }
 
    /**
     * @see com.xsm.gwt.widgets.client.Source.Consumer#setContent(com.xsm.gwt.widgets.client.Source, java.lang.String)
     */
    public void setContent(Source src, String content) {
        setHTML(content);
    }
    
    /**
     * @see com.xsm.gwt.widgets.client.Source.Consumer#failedContentLoad(com.xsm.gwt.widgets.client.Source, java.lang.Throwable)
     */
    public void failedContentLoad(Source src, Throwable x) {
        log("Failed to retreive HTML content from [" + htmlSrc + "]", x);
        setHTML(this.defaultHTML);
    }

    /**
     * Refreshes the HTML content from the Source.
     * 
     * author Sony Mathew
     */
    public void refresh() {
        htmlSrc.refresh();
    }
        
    private void log(String msg, Throwable x) {
        GWT.log(getClass().getName() + ">>" + msg, x);
    }
    
}
